import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.plugin.*;
import ij.text.TextWindow;
import ij.io.Opener;
import java.awt.*;
import java.io.*;
import java.net.*;

/** This plugin shows how files required for plugins can be packaged and
 * placed in a jar file. It loads and displays a text file and two image 
 * from a jar file named pcldemo.jar located in the plugins folder. 
 * 
 * It requires ImageJ 1.31o, which features an improved PluginClassLoader 
 * that can read class files and non .class resources (images, config
 * files, etc) from jar files in the plugins folder. Everything except 
 * the actual class that extends PlugIn can be placed in a jar file anywhere
 * in the plugins folder or immediate subfolders.
 * 
 * @author  Daniel Tyreus, CircuSoft Instrumentation LLC
 */
public class PCL_Demo implements PlugIn {

    public void run(String arg) {
        displayJpeg(); 
        displayTiff(); 
        displayText();   	
    }   

    //  Loads and displays a JPEG image from a JAR using the getResource() method. 
    // Should also work for GIF and PNG images.
    void displayJpeg() {
        URL url = null;
        try {
            url = getClass().getResource("/resources/buenavista.jpg");
            Image image = Toolkit.getDefaultToolkit().getImage(url);            
            // display the image
            ImagePlus imp = new ImagePlus("San Francisco (JPEG)", image);
            imp.show(); 
        }
        catch (Exception e) {
            String msg = e.getMessage();
            if (msg==null || msg.equals(""))
                msg = "" + e;	
            IJ.showMessage("PCL Demo", msg + "\n \n" + url);
        }
    }

    //  Loads and displays a TIFF from within a JAR archive using getResourceAsStream().
     void displayTiff() {
            InputStream is = PCL_Demo.class.getResourceAsStream("/resources/buenavista.tif");
            if (is!=null) {
                Opener opener = new Opener();
                ImagePlus imp = opener.openTiff(is, "San Francisco (TIFF)");
                if (imp!=null) imp.show();
            }
    }

    //  Loads and displays a text file from within a JAR archive using getResourceAsStream().
    void displayText() {
        try {
            // get the text resource as a stream
            InputStream is = PCL_Demo.class.getResourceAsStream("/resources/text.txt");
            InputStreamReader isr = new InputStreamReader(is);
            
            StringBuffer sb = new StringBuffer();
            char [] b = new char [8192];
            int n;
            //read a block and append any characters
            while ((n = isr.read(b)) > 0)
                sb.append(b,0, n);
            
            // display the text in a TextWindow
            new TextWindow("PluginClassLoader Demo", sb.toString(), 450, 450);
        }
        catch (IOException e) {
            String msg = e.getMessage();
            if (msg==null || msg.equals(""))
                msg = "" + e;	
            IJ.showMessage("PCL Demo", msg);
        }
         
    }


}

